#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f3f3f3f
#define mod 998244353
#define N 1000010
#define int long long
#define szi sizeof(int)
#define il inline
int T,n,a[N],s[N],mx[N],f[N],ans;
il void cmax(int &x,int y){x=(x>y)?x:y;return ;}
il void solve(){
    scanf("%lld",&n),ans=0;
    for(int i=1;i<=n;++i)
        scanf("%lld",&a[i]),f[i]=0,mx[a[i]]=-inf;
    for(int i=1;i<=n;++i)
        s[i]=(a[i]==a[i-1])?a[i]:0,s[i]+=s[i-1];
    f[1]=0,mx[0]=0;
    for(int i=2;i<=n;++i){
        f[i]=mx[0]+s[i-1],cmax(f[i],mx[a[i]]+s[i-1]+a[i]);
        cmax(mx[a[i-1]],f[i]-s[i]),cmax(mx[0],f[i]-s[i]);
    }
    for(int i=1;i<=n;++i) cmax(ans,f[i]+s[n]-s[i]);
    printf("%lld\n",ans);
    return ;
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%lld",&T); while(T--) solve();
    return 0;
}
